<?php
defined('LMO') or die('Can\'t access !');

class LFile 
{
	private function __constructor(){}
	
	public static function getListFile($dir, $fileExt = null)
	{
		$files = array();
		
		if(!is_dir($dir)) {
			return $files;
		}
		
		$fp = @opendir($dir);
		while (false !== ($entry = readdir($fp))) {
			if ($entry != "." && $entry != "..") {
				if($fileExt != null) {
					if(preg_match("/($fileExt)$/i", $entry)) {
						$files[] = $entry;
					}
				} else {
					$files[] = $entry;
				}
			}
		}
		closedir($fp);
		
		return $files;
	}
	
	public static function readFile($file)
	{
		$data = null;
		
		if(!is_file($file)) return $data;
		
		$fp = @fopen($file, "r");
		if ($fp) {
			while (!feof($fp)) {
				$buffer = fgets($fp, 4096);
				$data .= $buffer;
			}
			fclose($fp);
		}
		
		return $data;
	}
	
	public static function writeFile($file, $data, $mode = 'w+', $line = false)
	{	
		$fp = @fopen($file, $mode);
		if($fp) {
			if($line == true) {
				fwrite($fp, $data."\r\n");
			} else {
				fwrite($fp, $data.$line);
			}
			fclose($fp);
			
			return true;
		}
		return false;
	}
}
?>